﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace huaguwang
{
    //======================================================================
    //
    //        Copyright (C) 苏州逸仙软件    
    //        All rights reserved
    //
    //        filename :D_T_Video_Exp
    //        description :视频操作相关
    //
    //        created by 何庆 at  2010-12-22
    //
    //======================================================================
    public class D_T_Video_Exp : D_T_Video
    {
        public M_Page DRToMPage(DataRow dr, int len, int classlen, int nrlen, string url, string classurl, string dtFormat)
        {
            var model = new M_Page()
            {
                id = dr["id"].Exp_Trim(),
                name = dr["title"].Exp_Trim().Exp_LeftByt(len),
                title = dr["title"].Exp_Trim(),
                classid = dr["bid"].Exp_Trim(),
                classname = dr["catename"].Exp_Trim().Exp_LeftByt(classlen),
                classtitle = dr["catename"].Exp_Trim(),
                url = url.Exp_Format(dr["id"]),
                classurl = classurl.Exp_Format(dr["bid"]),
                date = dr["adddate"].Exp_DateTimeTryParse(),
                strdate = dr["adddate"].Exp_DateTimeTryParse().ToString(dtFormat),
                nr = dr["Explain"].Exp_Trim().Exp_RegexHTML().Exp_LeftByt(nrlen),
                click = dr["click"].Exp_Trim(),
                img1 = dr["pic"].Exp_Trim()
            };
            return model;

        }
        public enum OrderByType
        {
            date,
            hot,
            Topis
        }
        public string GetOrderByType(OrderByType e)
        {
            switch(e)
            {
                case OrderByType.date:
                    return "a.AddDate desc";
                case OrderByType.hot:
                    return "a.click desc";
                case OrderByType.Topis:
                    return "a.Topis desc ";
                default:
                    break;
            }
            return "a.id";
        }
        public List<M_Page> GetHotTop10(int len, int classlen, int nrlen, string url, string classurl, string dtFormat, int Top, OrderByType e)
        {
            var order = GetOrderByType(e);
            var strSql = " select Top " + Top + "  a.id,a.Title,a.AddDate,a.Explain,a.Click,a.Pic,bid=b.id,b.CateName " +
                        " from T_Video a " +
                        " inner join T_Category b on a.CategoryID=b.id " +
                        " where a.Flag=1 order by " + order;
            var item = MSSQLHelper.SQLConType.C1.Exp_GetList<M_Page>(strSql,
                dr => DRToMPage(dr, len, classlen, nrlen, url, classurl, dtFormat));
            return item;
        }

        /// <summary>
        /// 获得其他分集资料
        /// <example>未实现</example>
        /// </summary>
        /// <param name="id">分机id</param>
        /// <returns></returns>
        public List<M_Page> GetModel_List(int id)
        {
            var list = new List<M_Page>();


            for(int i = 2; i < 30; i++)
            {
                var model = new M_Page()
                {
                    id = id.ToString(),
                    level = i.ToString(),
                    url = id.ToString()
                };

                list.Add(model);
            }
            return list;
        }
        /// <summary>
        /// 获得一个实体
        /// <example>此方法未完成</example>
        /// </summary>
        /// <param name="id">分集的ｉｄ</param>
        /// <returns></returns>
        public M_Page GetModel_Page(int id)
        {
            var model = base.GetModelFlag(id);
            if(model == null)
            {
                return null;
            }
            else
            {
                var tempModel = new M_Page()
                {
                    b1 = "推荐",
                    b2 = "评论",
                    classid = (-1).ToString(),
                    classurl = (-1).ToString(),
                    id = model.id.ToString(),
                    url = model.id.ToString(),
                    classname = "暂无",
                    click = model.Click.ToString(),
                    hot = model.Click.ToString(),
                    img1 = model.Pic,
                    level = "暂无",
                    name = model.Title,
                    nr = model.Explain,
                    playurl = model.URL,
                    title = model.Title,
                };
                return tempModel;
            }

        }
        /// <summary>
        /// 分页获取数据量
        /// </summary>
        /// <returns></returns>
        public int GetPageCount(string txtVideoName, string cate, string type)
        {
            string strSQL = ExpandMSSQL.GetPageSQL(
                "dbo.T_Video  a inner join T_Category b on a.CategoryID = b.id ",
                "a.flag =1 and b.flag =1 and a.Title like '%'+@VideoName+'%'");
            if(cate != "")
            {
                strSQL = strSQL + " and a.CategoryID ='" + cate + "'";
            }
            if(type != "-1")
            {
                strSQL = strSQL + " and a.HasVideoCD =" + type;
            }
            SqlParameter[] parms = { 
                                   new SqlParameter("@VideoName",SqlDbType.NVarChar,100)
                                   };
            parms[0].Value = txtVideoName.Exp_Trim();
            var PageCount = MSSQLHelper.SQLConType.C1.Exp_GetSingle(strSQL, parms).Exp_IntTryParse();
            return PageCount;
        }

        /// <summary>
        /// 分页获取数据信息
        /// </summary>
        /// <param name="Begin">起始目录</param>
        /// <param name="Size">每页显示的数量</param>
        /// <returns></returns>
        public List<M_T_Video> GetPageList(int Begin, int Size, string txtVideoName, string cate, string type)
        {
            StringBuilder strwhere = new StringBuilder();
            strwhere.Append("a.flag =1 and b.flag =1 and a.Title like '%'+@VideoName+'%'");
            if(cate != "")
            {
                strwhere.Append(" and a.CategoryID ='" + cate + "'");
            }
            if(type != "-1")
            {
                strwhere.Append(" and a.HasVideoCD =" + type);
            }
            string strSQL = ExpandMSSQL.GetPageSQL(
                   Begin,
                   Size,
                   "a.id,a.Title,a.AddDate,a.Explain,b.CateName,a.HasVideoCD,c.RealName",
                   "T_Video a inner join T_Category b on a.CategoryID = b.id inner join T_AdminUser as c on a.UserID = c.id ",
                   "a.id",
                  strwhere.ToString(),
                   "a.AddDate desc");
            SqlParameter[] parms = { 
                                   new SqlParameter("@VideoName",SqlDbType.NVarChar,100)
                                   };
            parms[0].Value = txtVideoName.Exp_Trim();
            var List = MSSQLHelper.SQLConType.C1.Exp_GetList(strSQL,
                DR => new M_T_Video()
                {
                    id = DR["id"].Exp_IntTryParse(),
                    Title = DR["Title"].Exp_Trim(),
                    AddDate = DR["AddDate"].Exp_DateTimeTryParse(),
                    Explain = DR["Explain"].Exp_Trim().Exp_RegexHTML().Exp_SubStringByLength(56, ""),
                    ColorKey = DR["CateName"].Exp_Trim(),
                    HasVideoCD = DR["HasVideoCD"].Exp_BoolTryParse(),
                    ColorTitle = DR["RealName"].Exp_Trim()
                }, parms);
            return List;
        }

        /// <summary>
        ///   新增视频实体
        /// </summary>
        /// <param name="model">视频实体</param>
        /// <param name="blnHasVideoCD">是否有CD光盘</param>
        /// <param name="modelCD">光盘实体</param>
        /// <returns></returns>
        public int Add(huaguwang.M_T_Video model, bool blnHasVideoCD, huaguwang.M_T_VideoCD modelCD)
        {
            if(!blnHasVideoCD)//没有实体
            {
                return base.Add(model);
            }
            else
            {
                //事务方式
                var items = MSSQLHelper.SQLConType.C1.ExecuteSqlTran((com, tran) =>
                {
                    try
                    {
                        //插入视频信息
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into T_Video(");
                        strSql.Append("Title,CategoryID,URL,AddDate,Size,Type,Source,Explain,Grade,Gold,WebSummary,WebTitle,WebKey,InnerKey,HasVideoCD,ColorKey,ColorTitle,Topis,RemSign,Click,Pic,Flag,ExperInfotID,UserID)");
                        strSql.Append(" values (");
                        strSql.Append("@Title,@CategoryID,@URL,@AddDate,@Size,@Type,@Source,@Explain,@Grade,@Gold,@WebSummary,@WebTitle,@WebKey,@InnerKey,@HasVideoCD,@ColorKey,@ColorTitle,@Topis,@RemSign,@Click,@Pic,@Flag,@ExperInfotID,@UserID)");
                        strSql.Append(";select @@IDENTITY");
                        SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@URL", SqlDbType.NVarChar,100),
					new SqlParameter("@AddDate", SqlDbType.SmallDateTime),
					new SqlParameter("@Size", SqlDbType.Int,4),
					new SqlParameter("@Type", SqlDbType.NVarChar,50),
					new SqlParameter("@Source", SqlDbType.NVarChar,100),
					new SqlParameter("@Explain", SqlDbType.NText),
					new SqlParameter("@Grade", SqlDbType.Int,4),
					new SqlParameter("@Gold", SqlDbType.Money,8),
					new SqlParameter("@WebSummary", SqlDbType.NVarChar,100),
					new SqlParameter("@WebTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@WebKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@InnerKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@HasVideoCD", SqlDbType.Bit,1),
					new SqlParameter("@ColorKey", SqlDbType.NText),
					new SqlParameter("@ColorTitle", SqlDbType.NText),
					new SqlParameter("@Topis", SqlDbType.Int,4),
					new SqlParameter("@RemSign", SqlDbType.Bit,1),
					new SqlParameter("@Click", SqlDbType.Int,4),
					new SqlParameter("@Pic", SqlDbType.NVarChar,100),
					new SqlParameter("@Flag", SqlDbType.Bit,1),
					new SqlParameter("@ExperInfotID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4)};
                        parameters[0].Value = model.Title;
                        parameters[1].Value = model.CategoryID;
                        parameters[2].Value = model.URL;
                        parameters[3].Value = model.AddDate;
                        parameters[4].Value = model.Size;
                        parameters[5].Value = model.Type;
                        parameters[6].Value = model.Source;
                        parameters[7].Value = model.Explain;
                        parameters[8].Value = model.Grade;
                        parameters[9].Value = model.Gold;
                        parameters[10].Value = model.WebSummary;
                        parameters[11].Value = model.WebTitle;
                        parameters[12].Value = model.WebKey;
                        parameters[13].Value = model.InnerKey;
                        parameters[14].Value = model.HasVideoCD;
                        parameters[15].Value = model.ColorKey;
                        parameters[16].Value = model.ColorTitle;
                        parameters[17].Value = model.Topis;
                        parameters[18].Value = model.RemSign;
                        parameters[19].Value = model.Click;
                        parameters[20].Value = model.Pic;
                        //flag标记在增加的使用永远为true
                        parameters[21].Value = true;
                        parameters[22].Value = model.ExperInfotID;
                        parameters[23].Value = model.UserID;
                        com.Parameters.Add(parameters[0]);
                        com.Parameters.Add(parameters[1]);
                        com.Parameters.Add(parameters[2]);
                        com.Parameters.Add(parameters[3]);
                        com.Parameters.Add(parameters[4]);
                        com.Parameters.Add(parameters[5]);
                        com.Parameters.Add(parameters[6]);
                        com.Parameters.Add(parameters[7]);
                        com.Parameters.Add(parameters[8]);
                        com.Parameters.Add(parameters[9]);
                        com.Parameters.Add(parameters[10]);
                        com.Parameters.Add(parameters[11]);
                        com.Parameters.Add(parameters[12]);
                        com.Parameters.Add(parameters[13]);
                        com.Parameters.Add(parameters[14]);
                        com.Parameters.Add(parameters[15]);
                        com.Parameters.Add(parameters[16]);
                        com.Parameters.Add(parameters[17]);
                        com.Parameters.Add(parameters[18]);
                        com.Parameters.Add(parameters[19]);
                        com.Parameters.Add(parameters[20]);
                        com.Parameters.Add(parameters[21]);
                        com.Parameters.Add(parameters[22]);
                        com.Parameters.Add(parameters[23]);
                        com.CommandText = strSql.ToString();
                        int intID = com.ExecuteScalar().Exp_IntTryParse();//得到视频ID
                        com.Parameters.Clear();

                        if(0 >= intID)
                        {
                            tran.Rollback();
                            return -1;
                        }
                        model.id = intID;

                        //插入CD
                        strSql = strSql = new StringBuilder();
                        strSql.Append("insert into T_VideoCD(");
                        strSql.Append("CDName,VideoID,ISBN,Publish,Author,Explain,Price,Gold,Grade,WebTitle,WebSummary,WebKey,InnerKey,ColorTitle,ColorKey,StockNum,SellNum,Click,Flag,Pic)");
                        strSql.Append(" values (");
                        strSql.Append("@CDName,@VideoID,@ISBN,@Publish,@Author,@Explain,@Price,@Gold,@Grade,@WebTitle,@WebSummary,@WebKey,@InnerKey,@ColorTitle,@ColorKey,@StockNum,@SellNum,@Click,@Flag,@Pic)");
                        strSql.Append(";select @@IDENTITY");
                        parameters = new SqlParameter[]{
					new SqlParameter("@CDName", SqlDbType.NVarChar,50),
					new SqlParameter("@VideoID", SqlDbType.Int,4),
					new SqlParameter("@ISBN", SqlDbType.NVarChar,50),
					new SqlParameter("@Publish", SqlDbType.NVarChar,100),
					new SqlParameter("@Author", SqlDbType.NVarChar,50),
					new SqlParameter("@Explain", SqlDbType.NText),
					new SqlParameter("@Price", SqlDbType.Money,8),
					new SqlParameter("@Gold", SqlDbType.Money,8),
					new SqlParameter("@Grade", SqlDbType.Int,4),
					new SqlParameter("@WebTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@WebSummary", SqlDbType.NVarChar,1000),
					new SqlParameter("@WebKey", SqlDbType.NVarChar,100),
					new SqlParameter("@InnerKey", SqlDbType.NVarChar,100),
					new SqlParameter("@ColorTitle", SqlDbType.NText),
					new SqlParameter("@ColorKey", SqlDbType.NText),
					new SqlParameter("@StockNum", SqlDbType.Int,4),
					new SqlParameter("@SellNum", SqlDbType.Int,4),
					new SqlParameter("@Click", SqlDbType.Int,4),
					new SqlParameter("@Flag", SqlDbType.Bit,1),
					new SqlParameter("@Pic", SqlDbType.NVarChar,255)};
                        parameters[0].Value = modelCD.CDName;
                        parameters[1].Value = intID;
                        parameters[2].Value = modelCD.ISBN;
                        parameters[3].Value = modelCD.Publish;
                        parameters[4].Value = modelCD.Author;
                        parameters[5].Value = modelCD.Explain;
                        parameters[6].Value = modelCD.Price;
                        parameters[7].Value = modelCD.Gold;
                        parameters[8].Value = modelCD.Grade;
                        parameters[9].Value = modelCD.WebTitle;
                        parameters[10].Value = modelCD.WebSummary;
                        parameters[11].Value = modelCD.WebKey;
                        parameters[12].Value = modelCD.InnerKey;
                        parameters[13].Value = modelCD.ColorTitle;
                        parameters[14].Value = modelCD.ColorKey;
                        parameters[15].Value = modelCD.StockNum;
                        parameters[16].Value = modelCD.SellNum;
                        parameters[17].Value = modelCD.Click;
                        //flag标记在增加的使用永远为true
                        parameters[18].Value = true;
                        parameters[19].Value = modelCD.Pic;

                        com.Parameters.Add(parameters[0]);
                        com.Parameters.Add(parameters[1]);
                        com.Parameters.Add(parameters[2]);
                        com.Parameters.Add(parameters[3]);
                        com.Parameters.Add(parameters[4]);
                        com.Parameters.Add(parameters[5]);
                        com.Parameters.Add(parameters[6]);
                        com.Parameters.Add(parameters[7]);
                        com.Parameters.Add(parameters[8]);
                        com.Parameters.Add(parameters[9]);
                        com.Parameters.Add(parameters[10]);
                        com.Parameters.Add(parameters[11]);
                        com.Parameters.Add(parameters[12]);
                        com.Parameters.Add(parameters[13]);
                        com.Parameters.Add(parameters[14]);
                        com.Parameters.Add(parameters[15]);
                        com.Parameters.Add(parameters[16]);
                        com.Parameters.Add(parameters[17]);
                        com.Parameters.Add(parameters[18]);
                        com.Parameters.Add(parameters[19]);
                        com.CommandText = strSql.ToString();
                        intID = com.ExecuteScalar().Exp_IntTryParse();
                        com.Parameters.Clear();
                        if(0 >= intID)
                        {
                            tran.Rollback();
                            return -2;
                        }
                    }
                    catch
                    {
                        tran.Rollback();
                        return 0;
                    }
                    tran.Commit();
                    return model.id;
                });
                return items;
            }
        }

        public int Update(huaguwang.M_T_Video model, bool blnHasVideoCD, huaguwang.M_T_VideoCD modelCD)
        {
            if(!blnHasVideoCD)//没有实体
            {
                return base.Update(model);
            }
            else
            {
                //事务方式
                var items = MSSQLHelper.SQLConType.C1.ExecuteSqlTran((com, tran) =>
                {
                    try
                    {
                        //修改视频信息
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update T_Video set ");
                        strSql.Append("Title=@Title,");
                        strSql.Append("CategoryID=@CategoryID,");
                        strSql.Append("URL=@URL,");
                        strSql.Append("AddDate=@AddDate,");
                        strSql.Append("Size=@Size,");
                        strSql.Append("Type=@Type,");
                        strSql.Append("Source=@Source,");
                        strSql.Append("Explain=@Explain,");
                        strSql.Append("Grade=@Grade,");
                        strSql.Append("Gold=@Gold,");
                        strSql.Append("WebSummary=@WebSummary,");
                        strSql.Append("WebTitle=@WebTitle,");
                        strSql.Append("WebKey=@WebKey,");
                        strSql.Append("InnerKey=@InnerKey,");
                        strSql.Append("HasVideoCD=@HasVideoCD,");
                        strSql.Append("ColorKey=@ColorKey,");
                        strSql.Append("ColorTitle=@ColorTitle,");
                        strSql.Append("Topis=@Topis,");
                        strSql.Append("RemSign=@RemSign,");
                        strSql.Append("Click=@Click,");
                        strSql.Append("Pic=@Pic,");
                        strSql.Append("Flag=@Flag,");
                        strSql.Append("ExperInfotID=@ExperInfotID,");
                        strSql.Append("UserID=@UserID");
                        strSql.Append(" where id=@id");
                        SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@URL", SqlDbType.NVarChar,100),
					new SqlParameter("@AddDate", SqlDbType.SmallDateTime),
					new SqlParameter("@Size", SqlDbType.Int,4),
					new SqlParameter("@Type", SqlDbType.NVarChar,50),
					new SqlParameter("@Source", SqlDbType.NVarChar,100),
					new SqlParameter("@Explain", SqlDbType.NText),
					new SqlParameter("@Grade", SqlDbType.Int,4),
					new SqlParameter("@Gold", SqlDbType.Money,8),
					new SqlParameter("@WebSummary", SqlDbType.NVarChar,100),
					new SqlParameter("@WebTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@WebKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@InnerKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@HasVideoCD", SqlDbType.Bit,1),
					new SqlParameter("@ColorKey", SqlDbType.NText),
					new SqlParameter("@ColorTitle", SqlDbType.NText),
					new SqlParameter("@Topis", SqlDbType.Int,4),
					new SqlParameter("@RemSign", SqlDbType.Bit,1),
					new SqlParameter("@Click", SqlDbType.Int,4),
					new SqlParameter("@Pic", SqlDbType.NVarChar,100),
					new SqlParameter("@Flag", SqlDbType.Bit,1),
					new SqlParameter("@ExperInfotID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4)};
                        parameters[0].Value = model.id;
                        parameters[1].Value = model.Title;
                        parameters[2].Value = model.CategoryID;
                        parameters[3].Value = model.URL;
                        parameters[4].Value = model.AddDate;
                        parameters[5].Value = model.Size;
                        parameters[6].Value = model.Type;
                        parameters[7].Value = model.Source;
                        parameters[8].Value = model.Explain;
                        parameters[9].Value = model.Grade;
                        parameters[10].Value = model.Gold;
                        parameters[11].Value = model.WebSummary;
                        parameters[12].Value = model.WebTitle;
                        parameters[13].Value = model.WebKey;
                        parameters[14].Value = model.InnerKey;
                        parameters[15].Value = model.HasVideoCD;
                        parameters[16].Value = model.ColorKey;
                        parameters[17].Value = model.ColorTitle;
                        parameters[18].Value = model.Topis;
                        parameters[19].Value = model.RemSign;
                        parameters[20].Value = model.Click;
                        parameters[21].Value = model.Pic;
                        parameters[22].Value = true;
                        parameters[23].Value = model.ExperInfotID;
                        parameters[24].Value = model.UserID;
                        com.Parameters.Add(parameters[0]);
                        com.Parameters.Add(parameters[1]);
                        com.Parameters.Add(parameters[2]);
                        com.Parameters.Add(parameters[3]);
                        com.Parameters.Add(parameters[4]);
                        com.Parameters.Add(parameters[5]);
                        com.Parameters.Add(parameters[6]);
                        com.Parameters.Add(parameters[7]);
                        com.Parameters.Add(parameters[8]);
                        com.Parameters.Add(parameters[9]);
                        com.Parameters.Add(parameters[10]);
                        com.Parameters.Add(parameters[11]);
                        com.Parameters.Add(parameters[12]);
                        com.Parameters.Add(parameters[13]);
                        com.Parameters.Add(parameters[14]);
                        com.Parameters.Add(parameters[15]);
                        com.Parameters.Add(parameters[16]);
                        com.Parameters.Add(parameters[17]);
                        com.Parameters.Add(parameters[18]);
                        com.Parameters.Add(parameters[19]);
                        com.Parameters.Add(parameters[20]);
                        com.Parameters.Add(parameters[21]);
                        com.Parameters.Add(parameters[22]);
                        com.Parameters.Add(parameters[23]);
                        com.Parameters.Add(parameters[24]);
                        com.CommandText = strSql.ToString();
                        int intID = com.ExecuteNonQuery().Exp_IntTryParse();//
                        com.Parameters.Clear();

                        if(0 >= intID)
                        {
                            tran.Rollback();
                            return -1;
                        }

                        strSql = new StringBuilder();
                        strSql.Append("select count(1) from T_VideoCD where VideoID = " + model.id + " and Flag = 1 ");
                        com.CommandText = strSql.ToString();
                        int iRsult = com.ExecuteScalar().Exp_IntTryParse();
                        if(iRsult > 0)//如果已经存在CD信息就更新CD信息
                        {
                            strSql = strSql = new StringBuilder();
                            strSql.Append("update T_VideoCD set ");
                            strSql.Append("CDName=@CDName,");
                            strSql.Append("VideoID=@VideoID,");
                            strSql.Append("ISBN=@ISBN,");
                            strSql.Append("Publish=@Publish,");
                            strSql.Append("Author=@Author,");
                            strSql.Append("Explain=@Explain,");
                            strSql.Append("Price=@Price,");
                            strSql.Append("Gold=@Gold,");
                            strSql.Append("Grade=@Grade,");
                            strSql.Append("WebTitle=@WebTitle,");
                            strSql.Append("WebSummary=@WebSummary,");
                            strSql.Append("WebKey=@WebKey,");
                            strSql.Append("InnerKey=@InnerKey,");
                            strSql.Append("ColorTitle=@ColorTitle,");
                            strSql.Append("ColorKey=@ColorKey,");
                            strSql.Append("StockNum=@StockNum,");
                            strSql.Append("SellNum=@SellNum,");
                            strSql.Append("Click=@Click,");
                            strSql.Append("Flag=@Flag,");
                            strSql.Append("Pic=@Pic");
                            strSql.Append(" where VideoID=@id");
                            parameters = new SqlParameter[]{
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@CDName", SqlDbType.NVarChar,50),
					new SqlParameter("@VideoID", SqlDbType.Int,4),
					new SqlParameter("@ISBN", SqlDbType.NVarChar,50),
					new SqlParameter("@Publish", SqlDbType.NVarChar,100),
					new SqlParameter("@Author", SqlDbType.NVarChar,50),
					new SqlParameter("@Explain", SqlDbType.NText),
					new SqlParameter("@Price", SqlDbType.Money,8),
					new SqlParameter("@Gold", SqlDbType.Money,8),
					new SqlParameter("@Grade", SqlDbType.Int,4),
					new SqlParameter("@WebTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@WebSummary", SqlDbType.NVarChar,1000),
					new SqlParameter("@WebKey", SqlDbType.NVarChar,100),
					new SqlParameter("@InnerKey", SqlDbType.NVarChar,100),
					new SqlParameter("@ColorTitle", SqlDbType.NText),
					new SqlParameter("@ColorKey", SqlDbType.NText),
					new SqlParameter("@StockNum", SqlDbType.Int,4),
					new SqlParameter("@SellNum", SqlDbType.Int,4),
					new SqlParameter("@Click", SqlDbType.Int,4),
					new SqlParameter("@Flag", SqlDbType.Bit,1),
					new SqlParameter("@Pic", SqlDbType.NVarChar,255)};
                            parameters[0].Value = model.id;
                            parameters[1].Value = modelCD.CDName;
                            parameters[2].Value = model.id;
                            parameters[3].Value = modelCD.ISBN;
                            parameters[4].Value = modelCD.Publish;
                            parameters[5].Value = modelCD.Author;
                            parameters[6].Value = modelCD.Explain;
                            parameters[7].Value = modelCD.Price;
                            parameters[8].Value = modelCD.Gold;
                            parameters[9].Value = modelCD.Grade;
                            parameters[10].Value = modelCD.WebTitle;
                            parameters[11].Value = modelCD.WebSummary;
                            parameters[12].Value = modelCD.WebKey;
                            parameters[13].Value = modelCD.InnerKey;
                            parameters[14].Value = modelCD.ColorTitle;
                            parameters[15].Value = modelCD.ColorKey;
                            parameters[16].Value = modelCD.StockNum;
                            parameters[17].Value = modelCD.SellNum;
                            parameters[18].Value = modelCD.Click;
                            parameters[19].Value = true;
                            parameters[20].Value = modelCD.Pic;

                            com.Parameters.Add(parameters[0]);
                            com.Parameters.Add(parameters[1]);
                            com.Parameters.Add(parameters[2]);
                            com.Parameters.Add(parameters[3]);
                            com.Parameters.Add(parameters[4]);
                            com.Parameters.Add(parameters[5]);
                            com.Parameters.Add(parameters[6]);
                            com.Parameters.Add(parameters[7]);
                            com.Parameters.Add(parameters[8]);
                            com.Parameters.Add(parameters[9]);
                            com.Parameters.Add(parameters[10]);
                            com.Parameters.Add(parameters[11]);
                            com.Parameters.Add(parameters[12]);
                            com.Parameters.Add(parameters[13]);
                            com.Parameters.Add(parameters[14]);
                            com.Parameters.Add(parameters[15]);
                            com.Parameters.Add(parameters[16]);
                            com.Parameters.Add(parameters[17]);
                            com.Parameters.Add(parameters[18]);
                            com.Parameters.Add(parameters[19]);
                            com.Parameters.Add(parameters[20]);
                            com.CommandText = strSql.ToString();
                            intID = com.ExecuteNonQuery().Exp_IntTryParse();
                            com.Parameters.Clear();
                            if(0 >= intID)
                            {
                                tran.Rollback();
                                return -2;
                            }
                        }
                        else
                        {
                            strSql = new StringBuilder();
                            strSql.Append("insert into T_VideoCD(");
                            strSql.Append("CDName,VideoID,ISBN,Publish,Author,Explain,Price,Gold,Grade,WebTitle,WebSummary,WebKey,InnerKey,ColorTitle,ColorKey,StockNum,SellNum,Click,Flag,Pic)");
                            strSql.Append(" values (");
                            strSql.Append("@CDName,@VideoID,@ISBN,@Publish,@Author,@Explain,@Price,@Gold,@Grade,@WebTitle,@WebSummary,@WebKey,@InnerKey,@ColorTitle,@ColorKey,@StockNum,@SellNum,@Click,@Flag,@Pic)");
                            strSql.Append(";select @@IDENTITY");
                            parameters = new SqlParameter[] {
					new SqlParameter("@CDName", SqlDbType.NVarChar,50),
					new SqlParameter("@VideoID", SqlDbType.Int,4),
					new SqlParameter("@ISBN", SqlDbType.NVarChar,50),
					new SqlParameter("@Publish", SqlDbType.NVarChar,100),
					new SqlParameter("@Author", SqlDbType.NVarChar,50),
					new SqlParameter("@Explain", SqlDbType.NText),
					new SqlParameter("@Price", SqlDbType.Money,8),
					new SqlParameter("@Gold", SqlDbType.Money,8),
					new SqlParameter("@Grade", SqlDbType.Int,4),
					new SqlParameter("@WebTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@WebSummary", SqlDbType.NVarChar,1000),
					new SqlParameter("@WebKey", SqlDbType.NVarChar,100),
					new SqlParameter("@InnerKey", SqlDbType.NVarChar,100),
					new SqlParameter("@ColorTitle", SqlDbType.NText),
					new SqlParameter("@ColorKey", SqlDbType.NText),
					new SqlParameter("@StockNum", SqlDbType.Int,4),
					new SqlParameter("@SellNum", SqlDbType.Int,4),
					new SqlParameter("@Click", SqlDbType.Int,4),
					new SqlParameter("@Flag", SqlDbType.Bit,1),
					new SqlParameter("@Pic", SqlDbType.NVarChar,255)};
                            parameters[0].Value = modelCD.CDName;
                            parameters[1].Value = model.id;
                            parameters[2].Value = modelCD.ISBN;
                            parameters[3].Value = modelCD.Publish;
                            parameters[4].Value = modelCD.Author;
                            parameters[5].Value = modelCD.Explain;
                            parameters[6].Value = modelCD.Price;
                            parameters[7].Value = modelCD.Gold;
                            parameters[8].Value = modelCD.Grade;
                            parameters[9].Value = modelCD.WebTitle;
                            parameters[10].Value = modelCD.WebSummary;
                            parameters[11].Value = modelCD.WebKey;
                            parameters[12].Value = modelCD.InnerKey;
                            parameters[13].Value = modelCD.ColorTitle;
                            parameters[14].Value = modelCD.ColorKey;
                            parameters[15].Value = modelCD.StockNum;
                            parameters[16].Value = modelCD.SellNum;
                            parameters[17].Value = modelCD.Click;
                            //flag标记在增加的使用永远为true
                            parameters[18].Value = true;
                            parameters[19].Value = modelCD.Pic;

                            com.Parameters.Add(parameters[0]);
                            com.Parameters.Add(parameters[1]);
                            com.Parameters.Add(parameters[2]);
                            com.Parameters.Add(parameters[3]);
                            com.Parameters.Add(parameters[4]);
                            com.Parameters.Add(parameters[5]);
                            com.Parameters.Add(parameters[6]);
                            com.Parameters.Add(parameters[7]);
                            com.Parameters.Add(parameters[8]);
                            com.Parameters.Add(parameters[9]);
                            com.Parameters.Add(parameters[10]);
                            com.Parameters.Add(parameters[11]);
                            com.Parameters.Add(parameters[12]);
                            com.Parameters.Add(parameters[13]);
                            com.Parameters.Add(parameters[14]);
                            com.Parameters.Add(parameters[15]);
                            com.Parameters.Add(parameters[16]);
                            com.Parameters.Add(parameters[17]);
                            com.Parameters.Add(parameters[18]);
                            com.Parameters.Add(parameters[19]);
                            com.CommandText = strSql.ToString();
                            intID = com.ExecuteScalar().Exp_IntTryParse();
                            com.Parameters.Clear();
                            if(0 >= intID)
                            {
                                tran.Rollback();
                                return -3;
                            }
                        }
                    }
                    catch
                    {
                        tran.Rollback();
                        return 0;
                    }
                    tran.Commit();
                    return model.id;
                });
                return items;
            }
        }

        public int DelCDFlag(int videoID)
        {
            var strSql = new StringBuilder("update T_VideoCD set Flag = 0 where VideoID = @videoID ");
            SqlParameter[] parameters = {
					new SqlParameter("@videoID", SqlDbType.Int,4)
                  };
            parameters[0].Value = videoID;

            int rows = MSSQLHelper.SQLConType.C1.Exp_ExecuteSql(strSql.ToString(), parameters);
            if(rows > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
}
